(function($) {

	$.extend({
		'tabs' : {
			'panel' : {}
		}
	});

	$.extend($.fn.tabs.methods, {
		'getTabById' : function(jq, id) {
			var tabs = $.data(jq[0], 'tabs').tabs;
			for (var i = 0; i < tabs.length; i++) {
				var tab = tabs[i];
				if (tab.panel('options').id == id) {
					return tab;
				}
			}
			return null;
		},
		'selectById' : function(jq, id) {
			return jq.each(function() {
				var state = $.data(this, 'tabs');
				var opts = state.options;
				var tabs = state.tabs;
				var selectHis = state.selectHis;
				if (tabs.length == 0) {
					return;
				}
				var panel = $(this).tabs('getTabById', id); // get the panel to
															// be activated
				if (!panel) {
					return
				}
				var selected = $(this).tabs('getSelected');
				if (selected) {
					if (panel[0] == selected[0]) {
						return
					}
					$(this).tabs('unselect', $(this).tabs('getTabIndex', selected));
					if (!selected.panel('options').closed) {
						return
					}
				}
				panel.panel('open');
				var title = panel.panel('options').title; // the panel title
				selectHis.push(title); // push select history
				var tab = panel.panel('options').tab; // get the tab object
				tab.addClass('tabs-selected');
				// scroll the tab to center position if required.
				var wrap = $(this).find('>div.tabs-header>div.tabs-wrap');
				var left = tab.position().left;
				var right = left + tab.outerWidth();
				if (left < 0 || right > wrap.width()) {
					var deltaX = left - (wrap.width() - tab.width()) / 2;
					$(this).tabs('scrollBy', deltaX);
				} else {
					$(this).tabs('scrollBy', 0);
				}
				$(this).tabs('resize');
				opts.onSelect.call(this, title, $(this).tabs('getTabIndex', panel));
			});
		},
		'existsById' : function(jq, id) {
			return $(jq[0]).tabs('getTabById', id) != null;
		},
		'closeAll' : function(jq) {
			jq.each(function() {
				var $container = $(this);
				var $tabs = $container.tabs('tabs');
				for (var index = $tabs.length; index > 0; index--) {
					$container.tabs('close', index);
				}
			});
		},
		'closeLeft' : function(jq) {
			jq.each(function() {
				var $container = $(this);
				var $tabs = $container.tabs('tabs');
				var $tab = $container.tabs('getSelected');
				var current = $container.tabs('getTabIndex', $tab);
				for (var index = current - 1; index > 0; index--) {
					$container.tabs('close', index);
				}
				$container.tabs('select', 1);
			});
		},
		'closeRight' : function(jq) {
			jq.each(function() {
				var $container = $(this);
				var $tabs = $container.tabs('tabs');
				var $tab = $container.tabs('getSelected');
				var current = $container.tabs('getTabIndex', $tab);
				for (var index = $tabs.length; index > current; index--) {
					$container.tabs('close', index);
				}
			});
		},
		'closeExceptCurrent' : function(jq) {
			jq.each(function() {
				var $container = $(this);
				var $tabs = $container.tabs('tabs');
				var $tab = $container.tabs('getSelected');
				var current = $container.tabs('getTabIndex', $tab);
				for (var index = $tabs.length; index > current; index--) {
					$container.tabs('close', index);
				}
				for (var index = current - 1; index > 0; index--) {
					$container.tabs('close', index);
				}
				$container.tabs('select', 1);
			});
		},
		'closeCurrent' : function(jq) {
			jq.each(function() {
				var $container = $(this);
				var $tabs = $container.tabs('tabs');
				var $tab = $container.tabs('getSelected');
				var current = $container.tabs('getTabIndex', $tab);
				$container.tabs('close', current);
			});
		}
	});

	$.extend($.tabs, {
		'getContainer' : function(toolButton) {
			return $(toolButton).parent().closest('.tabs-container');
		}
	});

	$.extend($.tabs.panel, {
		'buttons' : {
			'refresh' : {
				iconCls : 'icon-mini-refresh',
				handler : function() {
				var $li = $(this).parent().parent();
					if($li.length > 0){
						var $tab = $('#tabContainer').tabs('getTab', $li.eq(0).index());
						$tab.panel('refresh');
					}
				}
			}
		}
	});

})(jQuery);
